﻿using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace Bilibili.Model.Dynamic
{
    public class DynamicCards
    {
        public DynamicCards Desc { get; set; }
        public int Type { get; set; }
        public long Rid { get; set; }
        public UserProfile User_Profile { get; set; }
        public long Dynamic_Id { get; set; }
        public int Repost { get; set; }
        public int Like { get; set; }
        public int Is_Liked { get; set; }
        public int Comment { get; set; }
        public long Orig_Dy_Id { get; set; }
        public int Orig_Type { get; set; }
        public string Card { get; set; }
        public string Mode
        {
            get
            {
                return Desc.Type switch
                {
                    4099 => "影视",
                    _ => "番剧",
                };
            }
        }
        public DynamicVideo Video { get; set; }
        public DynamicBangumi Bangumi { get; set; }
        public DynamicMusic Music { get; set; }
        public DynamicArticle Article { get; set; }
        public DynamicFeedNew Feed1 { get; set; }
        public DynamicWeb Web { get; set; }
        public DynamicFeed Feed { get; set; }
        public DynamicMiniVideo MiniVideo { get; set; }
        public Display Display { get; set; }
        private int _reply;
        public int Reply
        {
            get
            {
                if (Desc.Comment != 0)
                    return Desc.Comment;
                if (_reply != 0)
                    return _reply;
                if (!string.IsNullOrEmpty(Card))
                    return 0;
                var re = Regex.Match(Card, @"""reply\"":(.*?),").Groups[1].Value.Trim().ToInt32();
                if (re != 0)
                    return re;
                else
                {
                    var re1 = Regex.Match(Card, @"""reply_count\"":(.*?),").Groups[1].Value.Trim().ToInt32();
                    return re1 != 0 ? re1 : Regex.Match(Card, @"""replyCnt\"":(.*?),").Groups[1].Value.Trim().ToInt32();
                }
            }
            set
            {
                _reply = value;
            }
        }
        public long TimeStamp { get; set; }
        public string Time
        {
            get
            {
                DateTime dtStart = new DateTime(1970, 1, 1);
                TimeSpan toNow = TimeSpan.FromSeconds(TimeStamp);
                DateTime dt = dtStart.Add(toNow).ToLocalTime();
                TimeSpan span = DateTime.Now - dt;
                if (span.TotalDays > 3)
                    return dt.ToString("MM-dd");
                else if (span.TotalDays > 2)
                    return string.Format("前天");
                else if (span.TotalDays > 1)
                    return string.Format("昨天");
                else if (span.TotalHours > 1)
                    return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
                else if (span.TotalMinutes > 1)
                    return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
                else if (span.TotalSeconds >= 1)
                    return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
                else
                    return "1秒前";
            }
        }
        public DynamicCards Extension { get; set; }
        public VoteCfg Vote_Cfg { get; set; }
        public void HandleFeed(int type)
        {
            switch (type)
            {
                case 0:
                    {
                        if (Display != null && Display.Emoji_Info != null)
                            Feed.emoji_details = Display.Emoji_Info.emoji_details;
                        if (Feed.orig_type == 4)
                            Feed.text.dynamic_id = Feed.orig_dy_id;
                        if (Feed.orig_type == 2 && Feed.photo != null)
                        {
                            if (Feed.photo.pictures_count > 9)
                            {
                                var ls = Feed.photo.pictures.Take(9).ToList();
                                ls.Last().count = Feed.photo.pictures_count;
                                Feed.photo.showpictures = ls;
                            }
                            else
                                Feed.photo.showpictures = Feed.photo.pictures;
                        }
                        break;
                    }
                case 1:
                    {
                        if (Feed1 == null)
                        {
                            Feed1 = Card.ToObject<DynamicFeedNew>();
                            if (Display != null && Display.Emoji_Info != null)
                                Feed1.emoji_details = Display.Emoji_Info.emoji_details;
                            if (Desc != null && Desc.Dynamic_Id != 0)
                                Feed1.dynamic_id = Desc.Dynamic_Id;
                            if (Desc != null && Desc.Orig_Dy_Id != 0)
                                Feed1.dynamic_id = Desc.Orig_Dy_Id;
                        }
                        var count = Feed1.pictures_count;
                        if (Feed1.pictures != null)
                            Feed1.pictures.ForEach(x => x.c = count);
                        if (Feed1.pictures_count > 9)
                        {
                            var ls = Feed1.pictures.Take(9).ToList();
                            ls.Last().count = Feed1.pictures_count;
                            Feed1.showpictures = ls;
                        }
                        else
                            Feed1.showpictures = Feed1.pictures;
                        break;
                    }
            }
        }
    }
}